package com.store.pet.petstore.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.store.pet.petstore.R;
import com.store.pet.petstore.activity.OrderDetailActivity;
import com.store.pet.petstore.adapter.OrderAdapter;
import com.store.pet.petstore.bean.MessageBean;
import com.store.pet.petstore.bean.OrderBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;
import com.store.pet.petstore.util.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 购物车页面
 */
public class CartFragment extends BaseFragment {

    @BindView(R.id.recycler_cart)
    RecyclerView recyclerCart;

    private List<OrderBean.ContentBean.OrderListBean> list = new ArrayList<>();

    private OrderAdapter adapter;

    private Map<String,Object> map = new HashMap<>();

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_cart;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        recyclerCart.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerCart.addItemDecoration(new SpaceItemDecoration(30));
        adapter = new OrderAdapter(R.layout.item_cart_rv,list,getUserRole());
        recyclerCart.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            map.put("orderid",list.get(position).getId());
            switch (view.getId()){
                case R.id.ship:
                    //发货
                    OKHttpUtils.requestData(Constant.SHIP, map, new OKHttpUtils.ServerCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            MessageBean msg = GsonUtils.parseMessage(data);
                            if(msg.getCode() == 103){
                                loadData();  //刷新
                            }
                            ToastUtils.showShort(msg.getMsg());
                        }

                        @Override
                        public void onFailed(String message) {
                            ToastUtils.showShort("网络错误");
                        }
                    });
                    break;
                case R.id.determine:
                    //确认付款
                    OKHttpUtils.requestData(Constant.DETERMINE, map, new OKHttpUtils.ServerCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            MessageBean msg = GsonUtils.parseMessage(data);
                            if(msg.getCode() == 104){
                                loadData();  //刷新
                                ToastUtils.showShort("确认付款成功");
                            }else {
                                ToastUtils.showShort(msg.getMsg());
                            }
                        }

                        @Override
                        public void onFailed(String message) {
                            ToastUtils.showShort("网络错误");
                        }
                    });
                    break;
                case R.id.order_item:
                    //查看订单详情
                    Intent intent = new Intent(getActivity(),OrderDetailActivity.class);
                    intent.putExtra("order",list.get(position));
                    startActivity(intent);
                    break;
            }
        });

        loadData();
    }

    private void loadData() {
        list.clear();
        if(getUserRole() == 1){
            //买家订单
            map.put("buyer",getMyId());
            OKHttpUtils.requestData(Constant.BUYER_ORDER, map, new OKHttpUtils.ServerCallBack() {
                @Override
                public void onSuccess(String data) {
                    OrderBean bean = GsonUtils.parseOrderBean(data);
                    list.addAll(bean.getContent().getOrderList());
                    new Thread(() -> getActivity().runOnUiThread(() -> adapter.notifyDataSetChanged())).start();  //在主线程刷新界面UI
                }

                @Override
                public void onFailed(String message) {
                    ToastUtils.showShort("网络错误");
                }
            });
        }else if(getUserRole() == 2){
            //卖家订单
            map.put("sellerid",getMyId());
            OKHttpUtils.requestData(Constant.SELLER_ORDER, map, new OKHttpUtils.ServerCallBack() {
                @Override
                public void onSuccess(String data) {
                    OrderBean bean = GsonUtils.parseOrderBean(data);
                    list.addAll(bean.getContent().getOrderList());
                    new Thread(() -> getActivity().runOnUiThread(() -> adapter.notifyDataSetChanged())).start();  //在主线程刷新界面UI
                }

                @Override
                public void onFailed(String message) {
                    ToastUtils.showShort("网络错误");
                }
            });
        }
    }

}
